﻿using System;
using System.Collections.Generic;
using System.Linq;
using Company.Merchandising.BusinessLayer.DataEntities;
using Company.Merchandising.BusinessLayer.DataTransferObjects;
using Company.Merchandising.Utilities.LinqToEntities;
using Telerik.Web.UI;
using WebApplication.Code;

namespace WebApplication.Controls.Security.Roles
{
	public partial class RolesEditControl : EntityUserControl
	{
		public enum ShowItemMode
		{
			Edit,
			Add
		}

		private const string _sessionKeyAvailableFaculties = "AvailableFaculties";
		private const string _sessionKeyChosenFaculties = "ChosenFaculties";

		public IList<DTOFaculty> AvailableFaculties
		{
			get
			{
				return Session[_sessionKeyAvailableFaculties] as IList<DTOFaculty>;
			}

			set
			{
				Session[_sessionKeyAvailableFaculties] = value;
			}
		}

		public IList<DTOFaculty> ChosenFaculties
		{
			get
			{
				return Session[_sessionKeyChosenFaculties] as IList<DTOFaculty>;
			}

			set
			{
				Session[_sessionKeyChosenFaculties] = value;
			}
		}

		public DTORole EditedRole
		{
			get
			{
				DTORole dtoRrole = new DTORole();
				dtoRrole.Name = RoleNameTextBox.Text;
				dtoRrole.Description = RoleDescriptionTextBox.Text;
				return dtoRrole;
			}
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			GridEditableItem item = this.Parent.NamingContainer as GridEditableItem;
			if (item == null || item.DataItem == null)
			{
				return;
			}

			if (item.DataItem is GridInsertionObject)
			{
				UpdateButton.Visible = false;
				SetFaculties(true, null);
				return;
			}

			Role editedRole = EntityDataSourceExtensions.GetItemObject<Role>(item.DataItem);
			if (editedRole != null)
			{
				InsertButton.Visible = false;
				SetFaculties(false, editedRole);
				BindRoleToForm(editedRole);
				return;
			}
		}

		private void BindRoleToForm(Role editedRole)
		{
			RoleNameTextBox.Text = editedRole.Name;
			RoleDescriptionTextBox.Text = editedRole.Description;
		}

		private void SetFaculties(bool isInsertRoleMode, Role editedRole)
		{
			if (isInsertRoleMode)
			{
				IList<DTOFaculty> dtoFaculties = new List<DTOFaculty>();
				foreach (var faculty in ModelEntities.Faculty)
				{
					dtoFaculties.Add(new DTOFaculty { Id = faculty.Id, Name = faculty.Name, Description = faculty.Description });
				}

				AvailableFaculties = dtoFaculties;
				ChosenFaculties = new List<DTOFaculty>();
			}
			else
			{
				Role role = (from r in ModelEntities.Role where r.Id == editedRole.Id select r).First();
				if (!role.Faculties.IsLoaded)
				{
					role.Faculties.Load();
				}

				IList<DTOFaculty> availableFaculties = new List<DTOFaculty>();
				IList<DTOFaculty> chosenFaculties = new List<DTOFaculty>();
				foreach (var faculty in ModelEntities.Faculty)
				{
					if (role.Faculties.Contains(faculty))
					{
						chosenFaculties.Add(new DTOFaculty() { Id = faculty.Id, Name = faculty.Name, Description = faculty.Description });
					}
					else
					{
						availableFaculties.Add(new DTOFaculty() { Id = faculty.Id, Name = faculty.Name, Description = faculty.Description });
					}
				}

				AvailableFaculties = availableFaculties;
				ChosenFaculties = chosenFaculties;
			}
		}

		protected void AvailableFacultiesOnRowDrop(object sender, GridDragDropEventArgs e)
		{
			if (!string.IsNullOrEmpty(e.HtmlElement))
			{
				return;
			}

			if (e.DraggedItems[0].OwnerGridID != AvailableFacultiesGrid.ClientID)
			{
				return;
			}

			if (!((e.DestDataItem == null && ChosenFaculties.Count == 0) ||
				(e.DestDataItem != null && e.DestDataItem.OwnerGridID == ChosenFacultiesGrid.ClientID)))
			{
				return;
			}

			IList<DTOFaculty> chosenFaculties = ChosenFaculties;
			IList<DTOFaculty> availableFaculties = AvailableFaculties;

			foreach (GridDataItem draggedItem in e.DraggedItems)
			{
				DTOFaculty draggedFaculty = GetFaculty(AvailableFaculties, (int)draggedItem.GetDataKeyValue("Id"));
				if (draggedFaculty != null)
				{
					chosenFaculties.Add(draggedFaculty);
					availableFaculties.Remove(draggedFaculty);
				}
			}

			ChosenFaculties = chosenFaculties;
			AvailableFaculties = availableFaculties;
			AvailableFacultiesGrid.Rebind();
			ChosenFacultiesGrid.Rebind();
		}

		private static DTOFaculty GetFaculty(IEnumerable<DTOFaculty> facultiesToSearch, int facultyId)
		{
			return (from faculty in facultiesToSearch where faculty.Id == facultyId select faculty).FirstOrDefault();
		}

		protected void AvailableFacultiesNeedDataSource(object source, GridNeedDataSourceEventArgs e)
		{
			AvailableFacultiesGrid.DataSource = AvailableFaculties;
		}

		protected void ChosenFacultiesNeedDataSource(object source, GridNeedDataSourceEventArgs e)
		{
			ChosenFacultiesGrid.DataSource = ChosenFaculties;
		}

		protected void ChosenFacultiesOnRowDrop(object sender, GridDragDropEventArgs e)
		{
			if (!string.IsNullOrEmpty(e.HtmlElement))
			{
				return;
			}

			if (e.DraggedItems[0].OwnerGridID != ChosenFacultiesGrid.ClientID)
			{
				return;
			}

			if (!((e.DestDataItem == null && AvailableFaculties.Count == 0) ||
				(e.DestDataItem != null && e.DestDataItem.OwnerGridID == AvailableFacultiesGrid.ClientID)))
			{
				return;
			}

			IList<DTOFaculty> chosenFaculties = ChosenFaculties;
			IList<DTOFaculty> availableFaculties = AvailableFaculties;

			foreach (GridDataItem draggedItem in e.DraggedItems)
			{
				DTOFaculty draggedFaculty = GetFaculty(ChosenFaculties, (int)draggedItem.GetDataKeyValue("Id"));
				if (draggedFaculty != null)
				{
					availableFaculties.Add(draggedFaculty);
					chosenFaculties.Remove(draggedFaculty);
				}
			}

			ChosenFaculties = chosenFaculties;
			AvailableFaculties = availableFaculties;
			AvailableFacultiesGrid.Rebind();
			ChosenFacultiesGrid.Rebind();
		}
	}
}